package com.example.exercise.MysqlUtil;

import com.example.exercise.Base.SignBase;
import com.example.exercise.Base.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FindExist {

    private static Connection conn=null;
    private static PreparedStatement ps=null;//操作整合sql语句的对象
    private static ResultSet rs=null;//查询结果的集合

    //DBService 对象
    public static DBService dbService=null;

    public static int getRecord()
    {
        int result = -1;
        //结果存放集合
        //MySQL 语句
        String sql="select TIME from card ;";
        //获取链接数据库对象
        conn= Mysqlconnect.get().getConnection();

        try {
            if(conn!=null&&(!conn.isClosed())){
                ps= (PreparedStatement) conn.prepareStatement(sql);
                if(ps!=null){
                    rs= ps.executeQuery();
                    if(rs!=null){
                        while(rs.next()){

                             if (!rs.getString("TIME").equals("0"))
                             {
                                 result = 0;

                             }

                        }
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Mysqlconnect.closeAll(conn,ps,rs);//关闭相关操作
        return result ;


    }

    public static String getUserLose(String card)
    {
        String state = null;
        //结果存放集合
        //MySQL 语句
        String sql="select 是否挂失 from card where Card ='"+card+"';";
        //获取链接数据库对象
        conn= Mysqlconnect.get().getConnection();

        try {
            if(conn!=null&&(!conn.isClosed())){
                ps= (PreparedStatement) conn.prepareStatement(sql);
                if(ps!=null){
                    rs= ps.executeQuery();
                    if(rs!=null){
                        while(rs.next()){

                            state = rs.getString("是否挂失");

                        }
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Mysqlconnect.closeAll(conn,ps,rs);//关闭相关操作
        return state ;


    }

    public  static  int getUserCard(String card){
        //结果存放集合
        List<User> list=new ArrayList<User>();
        //MySQL 语句
        String sql="select Card from card where Card ='"+card+"';";
        int result = 0;
        //获取链接数据库对象
        conn= Mysqlconnect.get().getConnection();
        User u=new User();
        try {
            if(conn!=null&&(!conn.isClosed())){
                ps= (PreparedStatement) conn.prepareStatement(sql);
                if(ps!=null){
                    rs= ps.executeQuery();
                    if(rs!=null){
                        while(rs.next()){
                            u.setCardid(rs.getString("Card"));
                            list.add(u);
                        }
                        if (u.getCardid()!=null){
                            System.out.println(u.getCardid());
                            result =1;
                        }
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
       Mysqlconnect.closeAll(conn,ps,rs);//关闭相关操作
        return result;
    }

    public  static  int getUserName(String name,String classroom){
        //结果存放集合
        List<User> list=new ArrayList<User>();
        //MySQL 语句
        String sql="select Name,CLASS from card where Name = '"+name+"';";
        int result = 0;
        //获取链接数据库对象
        Mysqlconnect.get();
        conn= Mysqlconnect.getConnection();
        User u=new User();
        try {
            if(conn!=null&&(!conn.isClosed())){
                ps= (PreparedStatement) conn.prepareStatement(sql);
                if(ps!=null){
                    rs= ps.executeQuery();
                    if(rs!=null){
                        while(rs.next()){
                            u.setName(rs.getString("Name"));
                            u.setClassroom(rs.getString("Class"));
                            list.add(u);
                            System.out.println("88888888888888888"+u.getClassroom());
                        }
                        if (u.getName()!=null && u.getClassroom()!= null){

                            if (u.getName().equals(name)&& u.getClassroom().equals(classroom)){
                                System.out.println(u.getCardid());
                                result =1;
                            }

                        }else {

                            result = 0;
                        }

                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Mysqlconnect.closeAll(conn,ps,rs);//关闭相关操作
        return result;
    }


}
